package com.gaoyubo;

import com.gaoyubo.entity.Student;

import com.mongodb.client.result.DeleteResult;
import com.sun.javafx.collections.MappingChange;
import org.hamcrest.core.Is;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
@RunWith(SpringRunner.class)
public class Ted {
    @Autowired
    MongoTemplate mongoTemplate;
    @Test
    public void listTest(){
        Query query = new Query();
        query.addCriteria(Criteria.where("name").regex("^.*s.*$"));
        List<Student> list = mongoTemplate.find(query, Student.class);
        System.out.println(list);
    }
    @Test
    public void insertObject(){
        Student student = new Student();
        student.setName("66");
        student.setSex("男");
        student.setId("2");
        mongoTemplate.save(student,"t_sss1");
    }
    @Test
    public void deleteById(){
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is("2"));
        DeleteResult remove = mongoTemplate.remove(query,Student.class,"t_sss1");
        System.out.println(true);
    }
    @Test
    public void selectById(){
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is("2"));
        mongoTemplate.findById(query,Student.class);
        System.out.println(true);
    }
    @Test
    public void updateById(){
        Update update = new Update();
        update.set("name","sss");
        update.set("sex","女");
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is("2"));
        mongoTemplate.updateFirst(query,update,"t_sss1");
        System.out.println(true);
    }
    /**
     *   2019/12/17
     *   晴气温较凉
     *   心情不太好本来找了一上午没解决问题心情略微受到些许影响
     *   下午找老师问还被锤了一顿,本来锤就锤没啥大不了的但是他一直
     *   往我旧伤上锤,特别痛,锤的当时就想还手但是
     */

}
